import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { ShopService } from 'src/app/services/shop.service';
import * as constants from 'src/app/providers/Constants'
import { ToastService } from 'src/app/providers/toast/toast.service';
import { Storage } from 'src/app/providers/Storage';
@Component({
  selector: 'app-goods-search',
  templateUrl: './goods-search.page.html',
  styleUrls: ['./goods-search.page.scss'],
})
export class GoodsSearchPage implements OnInit {
  btn: string = 'Cancel'
  searchValue: String = ''
  searchValueInfo: any = [];
  array:any=[];
  constructor(private nav: NavController,private service:ShopService,private toast:ToastService) { }

  ngOnInit() {
    this.searchValueInfo=Storage.localStorage.get('searchValueInfo');
    if (this.searchValueInfo&&this.searchValueInfo.length>0) {
      this.searchValue=this.searchValueInfo[0];
    }
    this.searchChange();
    this.getSelHotProc();
  }
  search() {

  }

  getList() {
    this.service.getProcList({
      procName:this.searchValue
    }, { showLoading: false }).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
      
      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }
  back() {
    this.nav.pop();
  }
  searchChange() {
    if (this.searchValue) {
      this.btn = 'Search'
    } else {
      this.btn = 'Cancel'
    }

  }
  onClickBtn() {
    if (this.searchValue) {
      if (this.searchValueInfo) {
        let isIdentical=false;
        let isIdenticalIndex=null;
        for (let index = 0; index < this.searchValueInfo.length; index++) {
          const element = this.searchValueInfo[index];
          if (element==this.searchValue) {
            isIdentical=true;
            isIdenticalIndex=index;
          }
        }
        if (!isIdentical) {
          let s=[this.searchValue];
          this.searchValueInfo=s.concat(this.searchValueInfo);
        }else{
          this.searchValueInfo.splice(isIdenticalIndex,1)
          let s=[this.searchValue];
          this.searchValueInfo=s.concat(this.searchValueInfo);
        }
      }else{
        this.searchValueInfo=[this.searchValue];
      }
      Storage.localStorage.set('searchValueInfo',this.searchValueInfo);
      this.nav.navigateForward('goods-search-list', {
        queryParams: {
          searchType:1,
          goodsInfo:JSON.stringify({
            searchValue:this.searchValue
          })
        }
      });
    } else {
      this.nav.pop()
    }
  }
  onClickDelect(){
    Storage.localStorage.remove('searchValueInfo');
    this.searchValueInfo=null;
  }
  onClickBtn1(item) {
      this.nav.navigateForward('goods-search-list', {
        queryParams: {
          searchType:1,
          goodsInfo:JSON.stringify({
            searchValue:item.procName
          })
        }
      });
  }
 onClickBtn2(item) {
      this.nav.navigateForward('goods-search-list', {
        queryParams: {
          searchType:1,
          goodsInfo:JSON.stringify({
            searchValue:item
          })
        }
      });
  }


  getSelHotProc(){
    this.service.selHotProc({}).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        this.array=res.array;
      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }
}
